﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Xml;

namespace DoxygenBrowserAddin.Data
{
  public class DoxygenConfig
  {

    public DoxygenConfig()
    {
      DoxHtmlPath = string.Empty;
    }

    public bool Load(string strPath)
    {
      XmlDocument xmlDoc = new XmlDocument();
      xmlDoc.Load(strPath);

      return Load(xmlDoc);
    }

    public bool Load(XmlDocument xmlDoc)
    {
      int iVersion = getVersion(xmlDoc);

      XmlNode doxHtmlPathNode = xmlDoc.SelectSingleNode("/DoxygenConfig/DoxHtmlPath");

      this.DoxHtmlPath = doxHtmlPathNode.InnerText;

      if (iVersion > 1)
      {
        load_version2(xmlDoc);
      }
      return true;
    }

    private void load_version2(XmlDocument xmlDoc)
    {
      XmlNode useFramesPathNode = xmlDoc.SelectSingleNode("/DoxygenConfig/UseFrames");
      if (useFramesPathNode != null)
      {
        this.UseFrames = Convert.ToBoolean(useFramesPathNode.InnerText);
      }
    }

    private int getVersion(XmlDocument xmlDoc)
    {
      int iVersion = 1;
      XmlNode rootNode = xmlDoc.SelectSingleNode("/DoxygenConfig");
      if(rootNode.Attributes.Count != 0)
      {
        iVersion = Convert.ToInt32(rootNode.Attributes["Version"].Value);
      }
      return iVersion;
    }

    public bool Save(string strPath)
    {
      XmlDocument xmlDoc = new XmlDocument();
      XmlElement doxygenConfigXmlElem = xmlDoc.CreateElement("DoxygenConfig");
      xmlDoc.AppendChild(doxygenConfigXmlElem);

      XmlAttribute xmlAtt = xmlDoc.CreateAttribute("Version");
      xmlAtt.Value = "2";
      doxygenConfigXmlElem.Attributes.Append(xmlAtt);

      XmlElement doxHtmlPathXmlElem = xmlDoc.CreateElement("DoxHtmlPath");
      doxHtmlPathXmlElem.InnerText = this.DoxHtmlPath;
      doxygenConfigXmlElem.AppendChild(doxHtmlPathXmlElem);

      XmlElement useFramesXmlElem = xmlDoc.CreateElement("UseFrames");
      useFramesXmlElem.InnerText = this.UseFrames.ToString();
      doxygenConfigXmlElem.AppendChild(useFramesXmlElem);


      xmlDoc.Save(strPath);
      return true;
    }

    public bool HasDoxHtmlPath
    {
      get
      {
        return !string.IsNullOrEmpty(DoxHtmlPath);
      }
    }

    private string _doxHtmlPath;
    public string DoxHtmlPath
    {
      get { return _doxHtmlPath; }
      set { _doxHtmlPath = value; }
    }

    private bool _useFrames;
    public bool UseFrames
    {
      get { return _useFrames; }
      set { _useFrames = value; }
    }



  }
}
